﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Car.IServices;
using Yuebon.Car.Models;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.Commons.Tree;
using Yuebon.WebApp.Controllers;
using NPinyin;

namespace Yuebon.WebApp.Areas.Car.Controllers
{
    [Area("Car")]
    [Route("Car/[controller]/[action]")]
    public class AutoBrandMemberController : BusinessMemberController<AutoBrand, IAutoBrandService>
    {

        public AutoBrandMemberController(IAutoBrandService _iService) : base(_iService)
        {
            iService = _iService;
        }

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合
        /// </summary>
        /// <returns>指定对象的集合</returns>
        [HttpGet]
        public IActionResult GetAll()
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            //base.CheckAuthorized(AuthorizeKey.ListKey);
            string keywords = Request.Query["search"].ToString() == null ? "" : Request.Query["search"].ToString();
            string orderByDir = Request.Query["order"].ToString() == null ? "" : Request.Query["order"].ToString();
            string orderFlied = Request.Query["sort"].ToString() == "" ? "SortCode" : Request.Query["sort"].ToString();
            string where = "1=1";
            bool order = orderByDir == "asc" ? false : true;
            if (!string.IsNullOrEmpty(keywords))
            {
                where += string.Format(" and Title like '%{0}%' ", keywords);
            }
            List<AutoBrand> list = iService.GetListWhere(where).OrderBy(t => t.SortCode).ToList();
            var treeList = new List<TreeSelectModel>();
            foreach (AutoBrand item in list)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.Id;
                treeModel.text = item.Title;
                treeModel.parentId = "";
                treeList.Add(treeModel);
            }
            return ToJsonContent(treeList.TreeSelectJson());
        }
    }
}